#!/user/bin/env node

const log = console.log,
      fs = require('fs'),
      http = require('http');

http.createServer((req,res)=>{
  let html = fs.readFileSync('./index.html');
  log(req.url);
  if(req.url === '/'){
    res.end(html);
  }
  if(req.url === '/getlist'){
    let movieList = require('./data.js');
    for(let i=0;i<movieList.length;i++){
      if(movieList[i].movieRange === '暂无评分'){
        movieList.splice(i--,1);
      }
      movieList.sort(function(a,b){
        return b.movieRange - a.movieRange;
      });
    }
    res.writeHead(200,{
      'content-type':'text/html;charset=utf8'
    });
    movieList = JSON.stringify(movieList);
    console.log(movieList);
    res.end(movieList);
  }
}).listen(8080);
